import { observable, action, toJS } from 'mobx';
import { WeaTools } from 'ecCom';
import Api from '../apis/view';

const { Base64 } = WeaTools;
// 打印预览界面
export default class EmailPrintStore {
  @observable printIds = '0'; // 邮件id
  @observable baseInfo = {}; // 基本信息
  @observable mailContent = ''; // 邮件内容
  @observable allSpan = {};
  @observable btnDisplay = 'block';

  @observable params = {
    mailId: '',
    type: '',
  };

  @action('设置Button的显隐')
  setBtnDisplay = (val) => {
    this.btnDisplay = val;
  };

  @action('设置对应的打印id')
  setPrintId = (id) => {
    this.printIds = id;
  };

  @action('加载页面信息')
  doInit = (params = {}) => {
    let judge1 = false,
      judge2 = false,
      judge3 = false,
      judge4 = false;
    this.params.mailId = params.mailId;
    this.params.type = params.type;
    return new Promise((resovle) => {
      // 权限
      Api.getRight(this.params).then((data) => {
        const { viewRight } = data;
        judge1 = true;
        if (viewRight !== 1) return null;
      });

      // 获取邮件基本信息
      Api.getBase(this.params).then((data) => {
        const { viewRight, viewBean, status } = data;
        judge3 = true;
        if (viewRight == 1 && status === '1') {
          this.baseInfo = toJS(viewBean);
          // 收件人 发件人 信息
          if (viewBean.isShowBcc === 1) {
            Api.getSentTo(this.params).then((datas) => {
              const { allSpan } = datas;
              judge2 = true;
              if (datas.viewRight === 1 && datas.status === '1') {
                this.allSpan = allSpan;
              }
            });
          }
        }
      });

      // 获取邮件内容信息
      Api.getContent(this.params).then((data) => {
        const { viewRight, mailContent, status } = data;
        judge4 = true;
        if (viewRight === 1 && status === '1') {
          this.mailContent = Base64.decode(mailContent);
        }
      });
      let timer = setInterval(() => {
        if (judge1 && judge2 && judge3 && judge4) {
          resovle(true);
          clearInterval(timer);
        }
      }, 100);
    });
  };

  // 打印
  printEmail = () => {
    this.btnDisplay = 'none'; // 打印按钮隐藏
    let content = document.getElementsByClassName('email_print_content')[0];

    if (content) {
      document.getElementsByTagName('body')[0].style.height =
        content.offsetHeight;
    }
    // 获取iframe的window对象
    //   let iframe = document.getElementsByClassName('wea-email-content-iframe')[0].contentWindow;
    let img_length = document.getElementsByTagName('img').length;
     /* eslint-disable */
    let img_success = 0;
    let img_erro = 0;
    let img_all = 0;
    /* eslint-enable */
    let images = document.getElementsByTagName('img');
    if (img_length === 0) {
      setTimeout(() => {
        window.print();
        this.btnDisplay = 'block';
      }, 100);
    } else if (img_length > 0) {
      for (let image of images) {
        let oneImg = new Image();
        oneImg.src = image.src;
        /* eslint-disable */
        oneImg.onload = () => {
          img_success++;
          img_all++;
          if (img_all == img_length) {
            setTimeout(() => {
              //   this.iframeFocus();
              window.print();
              this.btnDisplay = "block";
            }, 100);
          }
        };
        oneImg.onerror = () => {
          img_erro++;
          img_all++;
          if (img_all == img_length) {
            setTimeout(() => {
              //    this.iframeFocus();
              window.print();
              // let preContent = document.getElementsByClassName('.email_print_content')[0].innerHTML || window.__preContent || '';
              // let printWindow = window.open('打印窗口','_blank');
              // printWindow.document.write(preContent);
              // printWindow.document.close();
              // printWindow.print();
              // printWindow.close();
              this.btnDisplay = "block";
            }, 100);
          }
        };
        /* eslint-enable */
      }
    }
  };

  // iframe焦点获取
  iframeFocus = () => {
    if (window.frames == null) {
      let iframe = document.getElementsByClassName(
        'wea-email-content-iframe',
      )[0];
      iframe.focus();
      iframe.contentWindow.print();
    } else {
      let iframe = document.getElementsByClassName(
        'wea-email-content-iframe',
      )[0];
      iframe.focus();

      iframe.contentWindow.print();

      window.document.body.innerHTML +=
        iframe.contentWindow.document.body.innerHTML;
      //   window.print();
    }
  };
}
